package com.crawler.crawler.service.impl;

import com.crawler.crawler.domain.Book;
import com.crawler.crawler.domain.BookImportConfig;
import com.crawler.crawler.service.BookImportConfigServer;
import com.crawler.crawler.service.BookService;
import com.crawler.crawler.service.ZyuCrawlerService;
import com.crawler.crawler.tool.zyu.ZyuBookInfo;
import com.crawler.crawler.tool.zyu.ZyuBookUrls;
import com.crawler.crawler.tool.zyu.pipeline.BookPipeline;
import com.crawler.crawler.tool.zyu.pipeline.DataPipeline;
import com.crawler.crawler.tool.zyu.ZyuPage;
import com.crawler.crawler.util.FileDownload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ZyuCrawlerServiceImpl implements ZyuCrawlerService {

    @Autowired
    private BookService bookService;

    @Autowired
    private BookImportConfigServer bookImportConfigServer;

    String filePath = "D:/E/zzyyuimages/";

    public String spider(){
        //类别数组
        String[] categoryArray={"novel","renwen","lishi","gudian","jisuanji","jingji","zhuanjirenwu"};


//        String[] categoryArray={"novel"};
        for (String category :categoryArray){
            String categoryUrl="http://www.zzyyu.com/index.php/category/"+category+"/";
            //得到页面数
            int pages=getPages(categoryUrl);

            //得到配置
            BookImportConfig zzyyuCategoryPage=bookImportConfigServer.selectBookByName("zyu_"+category);
            int startPage=Integer.parseInt(zzyyuCategoryPage.getValue());
            for (int page=startPage;page<=pages;page++){
               List<String> bookList= getBookList(page,categoryUrl);
               for(String bookUrl:bookList){
                   //查重
                   Book findBook=bookService.selectBookByUrl(bookUrl);
                   if(findBook!=null){
                       continue;
                   }
                   System.out.println("当前书的URL："+bookUrl);
                   getBookInfo(bookUrl,category);
               }
                BookImportConfig config=new BookImportConfig();
                config.setId(zzyyuCategoryPage.getId());
                config.setValue(String.valueOf(page));
                bookImportConfigServer.updateConfig(config);
                System.out.println("当前位置："+category+",页面:"+page+"，共"+pages+"页，还有"+(pages-page)+"页");
            }
        }

        //完成后重置
        for (String category :categoryArray){
            BookImportConfig zzyyuCategoryPage=bookImportConfigServer.selectBookByName("zyu_"+category);
            //完成后重置
            BookImportConfig config=new BookImportConfig();
            config.setId(zzyyuCategoryPage.getId());
            config.setValue("1");
            bookImportConfigServer.updateConfig(config);
        }

        return "成功";
    }

    /**
     * 得到总页数
     *
     * @return
     */
    @Override
    public int getPages(String url) {
        ZyuPage zyuPage=new ZyuPage();
        DataPipeline dataPipeline=  new DataPipeline();
        zyuPage.process(url,dataPipeline);
        Object object=dataPipeline.getObject();
        return Integer.parseInt(object.toString());
    }

    /**
     * 通过指定页的列表页面 得到书的链接集合
     *
     * @param page 指定页数
     * @return
     */
    @Override
    public List<String> getBookList(int page,String listUrl) {
        ZyuBookUrls zyuBookUrls=new ZyuBookUrls();
        if(page>1){
            listUrl=listUrl+"page/"+page+"/";
        }
        DataPipeline dataPipeline=  new DataPipeline();
        zyuBookUrls.process(listUrl,dataPipeline);
        List<String> list= (List<String>) dataPipeline.getObject();
        for (int i=0;i<list.size();i++){
            System.out.println(list.get(i));
        }
        return list;
    }

    /**
     * 得到图书信息并保存
     *
     * @param bookUrl
     */
    @Override
    public void getBookInfo(String bookUrl,String categor) {
        ZyuBookInfo zyuBookInfo=new ZyuBookInfo();
        BookPipeline bookPipeline=  new BookPipeline();
        zyuBookInfo.process(bookUrl,bookPipeline);
        Book book = bookPipeline.getBook();
        try {
            //下载图片
            String bookCover=FileDownload.downloadPicture(book.getBookCover(),filePath);
            //保存图片
            book.setBookCover(bookCover);
        }catch (Exception e){

        }

        book.setCat(getCatName(categor));
        book.setUrl(bookUrl);
        book.setSiteName("zzyyu");
        bookService.insertBook(book);
        System.out.println(book.toString());
    }

    /**
     * 图书分类
     * @param cat
     * @return
     */
    private String getCatName(String cat){
        Map<String,String> map=new HashMap<>();
        String[] categoryArray={"novel","renwen","lishi","gudian","jisuanji","jingji","zhuanjirenwu"};
        map.put("novel","小说文学");
        map.put("renwen","社科纪实");
        map.put("lishi","历史文化");
        map.put("gudian","古典文学");
        map.put("jisuanji","科学新知");
        map.put("jingji","商业经管");
        map.put("zhuanjirenwu","传记人物");
        return map.get(cat);
    }


}
